package cn.pfund.controller.admin;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.pfund.controller.BaseController;
import cn.pfund.entity.Product;
import cn.pfund.entity.ProductType;
import cn.pfund.entity.Tag;
import cn.pfund.service.ProductService;
import cn.pfund.service.ProductTypeService;
import cn.pfund.service.TagService;
import cn.pfund.util.Format;
import cn.pfund.util.Pagination;
import cn.pfund.util.ParamBean;

/**
 * 商品
 * @author zhaominglei qq:351024274，专注于网站、app和游戏开发。
 * @date 2017年1月20日
 */
@Controller("adminProductController")
@RequestMapping("/admin/product")
public class ProductController extends BaseController {
    @Resource(name="productService")
    private ProductService productService;
    @Resource(name="productTypeService")
    private ProductTypeService productTypeService;
    @Resource(name="tagService")
    private TagService tagService;
    
    @RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "admin/product/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<Product> pagination = productService.getPaginationAdmin(paramBean);
		request.setAttribute("pagination", pagination);
		return "admin/product/list";
	}
	
	@RequestMapping(value = "/add",method=RequestMethod.GET)
	public String add(HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("productTypes", productTypeService.getAllProductType());
		request.setAttribute("tags", tagService.getAllTag());
		return "admin/product/add";
	}
	
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	public String save(Product product, HttpServletRequest request, HttpServletResponse response, Model model){
		String productTypeId = request.getParameter("productTypeId");
		if (productTypeId == null || productTypeId.equals("")) {
			return ERROR_VIEW;
		}
		ProductType productType = productTypeService.getById(ProductType.class, Long.valueOf(productTypeId));
		if (productType == null) {
			return ERROR_VIEW;
		}
		product.setProductType(productType);
		Set<Tag> tags = new HashSet<Tag>();
		String[] tagIds = request.getParameterValues("tagIds");
		if (tagIds != null && tagIds.length > 0) {
			for (String tagId : tagIds) {
				Tag tag = tagService.getById(Tag.class, Long.valueOf(tagId));
				if (tag == null) {
					continue;
				}
				tags.add(tag);
			}
		}
		product.setTags(tags);
		product.setIsMarketable((product.getIsMarketable() != null && product.getIsMarketable()) ? true : false);
		productService.build(product);
		productService.save(product);
		return "redirect:/admin/product/list.shtml";
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("product", productService.getById(Product.class, id));
		request.setAttribute("productTypes", productTypeService.getAllProductType());
		request.setAttribute("tags", tagService.getAllTag());
		return "admin/product/edit";
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public String update(Product product, HttpServletRequest request, HttpServletResponse response, Model model){
		Product pProduct = productService.getById(Product.class, product.getId());
		if (pProduct == null) {
			return ERROR_VIEW;
		}
		String productTypeId = request.getParameter("productTypeId");
		if (productTypeId == null || productTypeId.equals("")) {
			return ERROR_VIEW;
		}
		ProductType productType = productTypeService.getById(ProductType.class, Long.valueOf(productTypeId));
		if (productType == null) {
			return ERROR_VIEW;
		}
		product.setProductType(productType);
		Set<Tag> tags = new HashSet<Tag>();
		String[] tagIds = request.getParameterValues("tagIds");
		if (tagIds != null && tagIds.length > 0) {
			for (String tagId : tagIds) {
				Tag tag = tagService.getById(Tag.class, Long.valueOf(tagId));
				if (tag == null) {
					continue;
				}
				tags.add(tag);
			}
		}
		product.setTags(tags);
		product.setIsMarketable((product.getIsMarketable() != null && product.getIsMarketable()) ? true : false);
		productService.build(product);
		product.setSource((product.getSource() == null || product.getSource().equals("") ? pProduct.getSource() : product.getSource()));
		productService.update(product);
		return "redirect:/admin/product/list.shtml";
	}
	
	@RequestMapping(value = "/view",method=RequestMethod.GET)
	public String view(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("product", productService.getById(Product.class, id));
		request.setAttribute("tags", tagService.getAllTag());
		return "admin/product/view";
	}

    @RequestMapping(value = "/delete",method=RequestMethod.GET)
    public String delete(String ids, HttpServletRequest request, HttpServletResponse response, Model model) {
        String[] idArray = ids.split(","); //支持批删 id逗号隔开
        for(String id:idArray){
        	Product product = productService.getById(Product.class, Long.valueOf(id));
        	if (product == null) continue;
        	productService.delete(product);
        }
        return "redirect:/admin/product/list.shtml";
    }	
}